package com.theme.passport.service.theme.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.theme.common.mvc.utils.PageUtils;
import com.theme.common.mvc.utils.Query;
import com.theme.common.mvc.view.R;
import com.theme.passport.dao.theme.ThemeDao;
import com.theme.passport.entity.SysUserEntity;
import com.theme.passport.entity.constant.ThemeAttchmentTypeEnum;
import com.theme.passport.entity.constant.ThemeStatusEnum;
import com.theme.passport.entity.theme.ThemeAttachmentEntity;
import com.theme.passport.entity.theme.ThemeEntity;
import com.theme.passport.entity.theme.WallpaperEntity;
import com.theme.passport.service.theme.ThemeAttachmentService;
import com.theme.passport.service.theme.ThemeService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;


@Service("themeService")
public class ThemeServiceImpl extends ServiceImpl<ThemeDao, ThemeEntity> implements ThemeService {



    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String userId = (String) params.get("userId");
        String keyword = (String) params.get("key");
        String status = (String) params.get("status");
        String free = (String) params.get("free");
        String nts = (String) params.get("nts");
        String classifyId = (String) params.get("classifyId");
        IPage<ThemeEntity> page = this.page(
                new Query<ThemeEntity>().getPage(params),
                new QueryWrapper<ThemeEntity>()
                        .eq(StringUtils.isNotBlank(nts),"nts", nts)
                        .eq(StringUtils.isNotBlank(free),"free", free)
                        .eq(StringUtils.isNotBlank(userId),"user_id", userId)
                        .eq(StringUtils.isNotBlank(classifyId), "classify_id", classifyId)
                        .in(StringUtils.isNotBlank(status), "status", status)
                        .like(StringUtils.isNotBlank(keyword), "name", keyword)
                        .orderByDesc("updated")
        );
        return new PageUtils(page);
    }

    @Override
    public PageUtils queryPageByStatus(Map<String, Object> params) {
        String status = (String) params.get("status");
        List<String> statusList = new ArrayList<>();
        if (StringUtils.isNotEmpty(status)){
            statusList.add(status);
        }else {
            statusList.add("1");
            statusList.add("2");
        }
        String keyword = (String) params.get("key");
        String classifyId = (String) params.get("classifyId");
        IPage<ThemeEntity> page = this.page(
                new Query<ThemeEntity>().getPage(params),
                new QueryWrapper<ThemeEntity>()
                        .in(statusList.size()>0,"status", statusList)
                        .eq(StringUtils.isNotBlank(classifyId), "classify_id", classifyId)
                        .like(StringUtils.isNotBlank(keyword), "title", keyword)
                        .orderByDesc("updated")
        );
        return new PageUtils(page);
    }

    @Override
    public R saveTheme(ThemeEntity theme) {

        theme.setCreated(new Date());
        theme.setUpdated(new Date());
        this.save(theme);

        return R.ok();
    }

    @Override
    public R updateTheme(ThemeEntity theme) {
        this.updateById(theme);
        return R.ok();
    }

    @Override
    public R deleteTheme(List<Long> asList) {
        this.removeByIds(asList);
        return R.ok();
    }

    @Override
    public PageUtils queryThemeByIds(Map<String, Object> params, List<Long> themeIds) {
        String status = (String) params.get("status");
        IPage<ThemeEntity> page = this.page(
                new Query<ThemeEntity>().getPage(params),
                new QueryWrapper<ThemeEntity>()
                        .eq(StringUtils.isNotBlank(status), "status", status)
                        .in(themeIds.size()>0,"id", themeIds)
                        .orderByDesc("updated")
        );
        return new PageUtils(page);
    }

}